import { Component, Input, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { CompanyService } from '@core/routes-services/company.service';
import { SysParamService } from '@core/routes-services/sys-param.service';

@Component({
  selector: 'step3',
  templateUrl: './step3.component.html',
})
export class Step3Component implements OnInit {


  entity: any = {};
  isLoading: boolean = false;
  validateForm: FormGroup;

  recordRatioObj: any = {};

  totalRatio: number = 0;

  constructor(private router: Router,
              private fb: FormBuilder,
              public companyService: CompanyService,
              public paramService: SysParamService,
              private message: NzMessageService) {
  }

  ngOnInit(): void {
    this.initTotalRatio();
    this.entity = this.companyService.entity;
    this.recordRatioObj = this.entity.recordRatioObj ? this.entity.recordRatioObj : {};
    console.log(this.recordRatioObj);
    this.initValidateForm();
  }

  initTotalRatio() {
    this.paramService.loadByCode('REWARD_ONLINE_PERCENT').then((res) => {
      console.log(res);
      this.totalRatio = res.data.value;
    }, err => {
      console.log(err);
    });
  }

  initValidateForm() {
    // 角色: 1-业务员，2-渠道商，3-门店，4-代理 自购、直接、间接积分比例
    this.isLoading = true;
    this.validateForm = this.fb.group({});
    this.validateForm = this.fb.group({
      role1: [this.recordRatioObj.role1, [Validators.required]],
      role2: [this.recordRatioObj.role2, [Validators.required]],
      role3_4: [this.recordRatioObj.role3_4, [Validators.required]],
      levelSelf: [this.recordRatioObj.levelSelf, [Validators.required]],
      levelOne: [this.recordRatioObj.levelOne, [Validators.required]],
      levelTwo: [this.recordRatioObj.levelTwo, [Validators.required]],
      levelThird: [this.recordRatioObj.levelThird, [Validators.required]],
      red_role1: [this.recordRatioObj.red_role1, [Validators.required]],
      red_role2: [this.recordRatioObj.red_role2, [Validators.required]],
      red_role3_4: [this.recordRatioObj.red_role3_4, [Validators.required]],
      red_levelSelf: [this.recordRatioObj.red_levelSelf, [Validators.required]],
      red_levelOne: [this.recordRatioObj.red_levelOne, [Validators.required]],
      red_levelTwo: [this.recordRatioObj.red_levelTwo, [Validators.required]],
    });
    this.isLoading = false;
  }

  submitForm(): void {
    let $this = this;
    if (this.validateForm.invalid) {
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
      }
    } else {
      //修改
      let redMaxRatio = this.validateForm.value.red_levelSelf + this.validateForm.value.red_levelOne + this.validateForm.value.red_levelTwo + this.validateForm.value.red_role1;
      if (redMaxRatio > this.totalRatio) {
        this.message.warning('当前现金奖励总比率大于配置最大比率：' + this.totalRatio + '，请修改合适的比率');
        return;
      }
      this.isLoading = true;
      if (!this.entity.id || this.entity.id == 0) {
        //新增 保存全部信息
        this.companyService.entity.recordRatioValue = this.validateForm.value;
        this.companyService.saveEntity(this.entity).then((res) => {
          if (res.status.toString().startsWith('20')) {
            this.message.success('保存成功');
            this.isLoading = false;
            this.initValidateForm();
            this.goBack();//gy
          } else {
            this.message.warning(res.message);
          }
          this.isLoading = false;
        }, error => {
          this.message.error(error.message ? error.message : error.error);
          this.isLoading = false;
        });
      } else {
        if (this.validateForm.dirty) {
          this.companyService.updatePrices(this.entity.id, { 'recordRatio': this.validateForm.value }).then((res) => {
            if (res.status.toString().startsWith('20')) {
              this.message.success('保存成功');
              this.isLoading = false;
              this.initValidateForm();
              this.goBack();
            } else {
              this.message.warning(res.message);
            }
            this.isLoading = false;
          }, error => {
            this.message.error(error.message ? error.message : error.error);
            this.isLoading = false;
          });
        } else {
          this.goBack();
        }
      }
    }
  }


  prev() {
    --this.companyService.step;
  }

  goBack() {
    this.router.navigate(['/company/list']);
  }


}
